<?php
/**
 * Created by JiangJiaCai.
 * User: LongBob
 * Date: 17-9-24
 * Time: 下午7:33
 */

namespace ext\service;


use App\Models\QdcsProject;
use com_jjcbs\lib\Service;
use com_jjcbs\lib\ServiceFactory;
use ext\dao\ProjectDaoImpl;
use Illuminate\Support\Facades\DB;

class ProjectServiceImpl extends Service
{
    public $projectDao = null;

    public function __construct()
    {
        $this->projectDao = new ProjectDaoImpl();
    }

    public function exec()
    {
        // TODO: Implement exec() method.
    }


    public function getAllChildProjectList(int $pid , int $limit){
        $ids = array_keys(ServiceFactory::getInstance(ClassServiceImpl::class)->getChildOption($pid));
        return QdcsProject::whereNotNull('img')->whereIn('ptype_id' , $ids)->orderByDesc('created_at')->limit($limit)->get();
    }

    public function getAllChildProjectListPage(int $pid ,int $pageSize = 6){
        $ids = array_keys(ServiceFactory::getInstance(ClassServiceImpl::class)->getChildOption($pid));
        if ( empty($ids)){
            return $this->getListPage($pid , $pageSize);
        }else{
            //父级
            return QdcsProject::whereNotNull('img')->whereIn('ptype_id' , $ids)->orderByDesc('created_at')->paginate($pageSize);
        }
    }

    public function getListPage(int $pid , int $pageSize = 6){
        return QdcsProject::where('ptype_id' , $pid)->paginate($pageSize);
    }

    public function getPos(QdcsProject $project){
        $classService = ServiceFactory::getInstance(ClassServiceImpl::class);
        return $classService->getClassPath($project->ptype_id , ' > ') . ' > ' . $project->name;
    }
}